package com.xbongbong.saas.enums.product;

import com.xbongbong.paas.enums.BISubProductCompareFieldEnum;
import com.xbongbong.pro.statistic.constant.RelatedProductConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import lombok.Getter;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author: 汪腾伟
 * @date: Created in 2022/7/18 16:50
 * @description：  关联产品字段可见权限需求，现在要支持关联产品字段编辑进审批，所以需要进行字段值前后比较；可以参考下面之前bi那边整理的字段
 * @see BISubProductCompareFieldEnum
 */
@Getter
public enum BusinessProductCompareEnum {
    /**
     *  销售机会
     */
    SALES_OPPORTUNITY(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(), Arrays.asList(RelatedProductConstant.PRODUCT_ID, RelatedProductConstant.PRODUCT_UNIT, RelatedProductConstant.PRODUCT_NUM, RelatedProductConstant.HISTORICAL_PRICE,
            RelatedProductConstant.DISCOUNT, RelatedProductConstant.PRICE, RelatedProductConstant.MEMO),
            Arrays.asList(OpportunityProductEnum.PRODUCT.getAttr(), OpportunityProductEnum.UNIT.getAttr(), OpportunityProductEnum.NUM.getAttr(),
                    OpportunityProductEnum.PRICE.getAttr(), OpportunityProductEnum.DISCOUNT.getAttr(), OpportunityProductEnum.SELLING_PRICE.getAttr(),
                    OpportunityProductEnum.MEMO.getAttr()), Arrays.asList(OpportunityProductEnum.PRICE.getAttr(), OpportunityProductEnum.SELL_TOTAL.getAttr())),
    /**
     * 报价单
     */
    QUOTATION(XbbRefTypeEnum.QUOTATION.getCode(), Arrays.asList(RelatedProductConstant.PRODUCT_ID, RelatedProductConstant.PRODUCT_UNIT, RelatedProductConstant.PRODUCT_NUM, RelatedProductConstant.HISTORICAL_PRICE,
            RelatedProductConstant.DISCOUNT, RelatedProductConstant.PRICE, RelatedProductConstant.MEMO),
            Arrays.asList(QuotationProductEnum.PRODUCT.getAttr(), QuotationProductEnum.UNIT.getAttr(), QuotationProductEnum.NUM.getAttr(),
                    QuotationProductEnum.PRICE.getAttr(), QuotationProductEnum.DISCOUNT.getAttr(), QuotationProductEnum.SELLING_PRICE.getAttr(),
                    QuotationProductEnum.MEMO.getAttr()), Arrays.asList(QuotationProductEnum.PRICE.getAttr(), QuotationProductEnum.SUBTOTAL.getAttr())),
    /**
     * 合同订单
     */
    CONTRACT(XbbRefTypeEnum.CONTRACT.getCode(), Arrays.asList(RelatedProductConstant.PRODUCT_ID, RelatedProductConstant.PRODUCT_UNIT, RelatedProductConstant.PRODUCT_NUM, RelatedProductConstant.HISTORICAL_PRICE,
            RelatedProductConstant.DISCOUNT,RelatedProductConstant.PRICE ,RelatedProductConstant.MEMO),
            Arrays.asList(ContractProductEnum.PRODUCT.getAttr(), ContractProductEnum.UNIT.getAttr(), ContractProductEnum.NUM.getAttr(),
                    ContractProductEnum.PRICE.getAttr(), ContractProductEnum.DISCOUNT.getAttr(), ContractProductEnum.SELLING_PRICE.getAttr(),
                    ContractProductEnum.MEMO.getAttr()), Arrays.asList(ContractProductEnum.PRICE.getAttr(), ContractProductEnum.SUBTOTAL.getAttr())),
    /**
     * 退货退款
     */
    REFUND(XbbRefTypeEnum.REFUND.getCode(), Arrays.asList(RelatedProductConstant.PRODUCT_ID, RelatedProductConstant.PRODUCT_UNIT, RelatedProductConstant.PRODUCT_NUM,
            RelatedProductConstant.PRICE, RelatedProductConstant.MEMO),
            Arrays.asList(RefundProductEnum.PRODUCT.getAttr(), RefundProductEnum.UNIT.getAttr(), RefundProductEnum.NUM.getAttr(),
                    RefundProductEnum.PRICE.getAttr(), RefundProductEnum.MEMO.getAttr()), Arrays.asList(RefundProductEnum.SUBTOTAL.getAttr(),
            RefundProductEnum.SOURCE_DOC.getAttr(), RefundProductEnum.BATCH.getAttr(), RefundProductEnum.PRODUCE_DATE.getAttr(), RefundProductEnum.GUARANTEE_PERIOD.getAttr(),
            RefundProductEnum.EXPIRE_DATE.getAttr())),
    ;


    /**
     * 主业务业务类型
     */
    private Integer businessType;
    /**
     * data里面的字段列表
     */
    private List<String> dataFields;
    /**
     * 与data里面字段对应的宽表里面的字段列表
     */
    private List<String> dbFields;

    /**
     * 不能编辑的字段列表
     */
    private List<String> unEditableFieldList;

    BusinessProductCompareEnum(Integer businessType, List<String> dataFields, List<String> dbFields, List<String> unEditableFieldList) {
        this.businessType = businessType;
        this.dataFields = dataFields;
        this.dbFields = dbFields;
        this.unEditableFieldList = unEditableFieldList;
    }

    /**
     * 根据业务类型获取关联产品的相关信息
     *
     * @param businessType 业务类型
     * @return
     */
    public static BusinessProductCompareEnum  getEnumByBusinessType (Integer businessType) {
        for (BusinessProductCompareEnum businessProductCompareEnum : BusinessProductCompareEnum.values()) {
            if (Objects.equals(businessProductCompareEnum.getBusinessType(), businessType)) {
                return businessProductCompareEnum;
            }
        }
        return null;
    }

    /**
     * 根据业务类型获取不能编辑的字段
     *
     * @param businessType 业务类型
     * @return
     */
    public static List<String> getAllUnEditableFieldList(Integer businessType) {
        BusinessProductCompareEnum businessProductCompareEnum = getEnumByBusinessType(businessType);
        if (Objects.isNull(businessProductCompareEnum)) {
            return new ArrayList<>();
        }else {
            return CollectionUtils.isEmpty(businessProductCompareEnum.getUnEditableFieldList()) ? new ArrayList<>() :  businessProductCompareEnum.getUnEditableFieldList();
        }
    }

    /**
     * 获取工作流需要处理的产品业务
     *
     * @return
     */
    public static List<Integer> getProductHandleBusList() {
        List<Integer> businessTypeList = new ArrayList<>();
        for (BusinessProductCompareEnum businessProductCompareEnum : BusinessProductCompareEnum.values()) {
            businessTypeList.add(businessProductCompareEnum.getBusinessType());
        }
        return businessTypeList;
    }

}
